﻿/****************************** Module Header ******************************\
 * Module Name:  SoDateTimePicker.cs
 * Module Version:  1.0.0 (2012-10-22)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCL.Forms.Components
{
    public partial class SoDateTimePicker : UserControl
    {
        /// <summary>
        /// 获取或设置是否显示秒钟
        /// </summary>
        public bool ShowSecond { get { return nudSecond.Visible; } set { nudSecond.Visible = value; tipSecond.Visible = value; ResizeControlSize(); } }

        /// <summary>
        /// 获取或设置是否显示分钟
        /// </summary>
        public bool ShowMinute { get { return nudMinute.Visible; } set { nudMinute.Visible = value; tipMinute.Visible = value; ResizeControlSize(); } }

        /// <summary>
        /// 获取或设置是否显示小时
        /// </summary>
        public bool ShowHour { get { return nudHour.Visible; } set { nudHour.Visible = value; tipMinute.Visible = value; ResizeControlSize(); } }

        /// <summary>
        /// 获取或设置是否显示日期
        /// </summary>
        public bool ShowDate { get { return dtpDate.Visible; } set { dtpDate.Visible = value; ResizeControlSize(); } }

        /// <summary>
        /// 获取或设置控件的值
        /// </summary>
        public DateTime Value { get { return GetValue(); } set { SetValue(value); } }

        public SoDateTimePicker()
        {
            InitializeComponent();
        }

        
        int width;
        // 重设控件尺寸 2014-2-18 by sutroon
        private void ResizeControlSize()
        {
            // dtpDate,nudHour,tipMinute,nudMinute,tipSecond,nudSecond
            width = 0;
            if (dtpDate.Visible) width += dtpDate.Width;
            if (nudHour.Visible) width += nudHour.Width;
            if (tipMinute.Visible) width += tipMinute.Width;
            if (nudMinute.Visible) width += nudMinute.Width;
            if (tipSecond.Visible) width += tipSecond.Width;
            if (nudSecond.Visible) width += nudSecond.Width;
            this.Width = width;
        }

        // 设置值
        private void SetValue(DateTime dt)
        {
            if (dt == DateTime.Parse("1900/1/1")) dt = DateTime.Now;
            dtpDate.Text = dt.ToShortDateString();
            nudHour.Value = dt.Hour;
            nudMinute.Value = dt.Minute;
            nudSecond.Value = dt.Second;
        }
        
        // 获取值
        private DateTime GetValue()
        {
            StringBuilder sb = new StringBuilder();
            if (ShowDate) sb.Append(dtpDate.Text);
            if (ShowHour) sb.AppendFormat(" {0}", nudHour.Value);
            if (ShowMinute) sb.AppendFormat(":{0}", nudMinute.Value);
            if (ShowSecond) sb.AppendFormat(":{0}", nudSecond.Value);
            return sb.ToString().TrimStart(' ', ':').ToDateTime();
        }
    }
}
